import { saveApply } from '@/api/vmsHzqrdApply.js'
import { saveConfirm } from '@/api/vmsHzqrdConfirm.js'
let validRedInvoiceSaveOrSubmit = async(context)=>{
    let handler = (context.$refs.hzqrdsq || context.$refs.hzqrdsqbc)[0]
    let valid = await handler.$refs.hzqrdsqRef.validate().catch((err)=>{
        console.log(err);
        return false
    })
    if (valid) {
        let params = {
            "vmsApprovalHzqrdDto": {
              "applyId": handler.$store.state.invoice2.invoinceFormStart1.busId, // 审批流id invoinceFormStart1
              "applyType": 0, // 0是红字确认单申请, 1是红字确认单确认
              "chlx": handler.vmsApprovalHzqrdSimpleVo.chlx,
              "chyydm": handler.vmsApprovalHzqrdSimpleVo.chyydm,
              "gmfsfdkrz": handler.vmsApprovalHzqrdSimpleVo.gmfsfdkrz,
              "gmfwdkyy": handler.vmsApprovalHzqrdSimpleVo.gmfwdkyy,
              "lrfsf": handler.vmsApprovalHzqrdSimpleVo.lrfsf,
              "lzfpdm": "", // 没有就不传递
              "lzfphm": handler.vmsApprovalHzqrdSimpleVo.lzfphm,
              "sfzzfpbz": handler.vmsApprovalHzqrdSimpleVo.sfzzfpbz
            },
            "vmsApprovalHzqrdItemDtoList": handler.vmsApprovalHzqrdSimpleVo.vmsApprovalHzqrdItemVoList
          }
        let {code} = await  saveApply(params);
        return code ==200
      } else {
        console.log('error submit!!');
        return false;
      }
}

let validRedInvoiceSaveOrSubmitConfirm = async(context)=>{
    console.log(context.$refs);
    let handler = ( context.$refs.hzqrdqr  || context.$refs.hzqrdqrsh
      )[0];
    let valid = await handler.$refs.hzqrdspRef.validate().catch((err)=>{
        console.log(err);
        return false
    })
    if (valid) {
        let params = {
            "applyId": handler.$store.state.invoice2.invoinceFormStart1.busId,
            "hzxxqrdbh": handler.vmsApprovalHzqrdSimpleVo.hzfpxxqrdbh
          }
        let {code} = await  saveConfirm(params);
        return code ==200
      } else {
        console.log('error submit!!');
        return false;
      }
    return true
}

const validFn = {
    "FLOW_HZQRDSQ":validRedInvoiceSaveOrSubmit,
    "FLOW_HZQRDQR":validRedInvoiceSaveOrSubmitConfirm,
}

export function beforeSaveValid(busType,context){
    return new Promise(async (resolve,reject)=>{
        if(busType && validFn[busType]){
          try {
            let res = await validFn[busType](context);
            resolve(res)
          } catch (error) {
            resolve(true)
          }

        }else{
            resolve(true)
        }

    })
    
}